﻿using System.Configuration;
using System.Text;
using System.Collections.Specialized;
using System;
using NLog;


namespace DGen.Reflection
{
    /// <summary>
    /// Settings helper
    /// </summary>
    public class SettingsHelper
    {
		private static Logger logger = LogManager.GetCurrentClassLogger ();
         private static string ShowAppSettings()
        {
        	System.Text.StringBuilder sb = new StringBuilder();
            AppSettingsReader reader = 
                new AppSettingsReader();


            NameValueCollection appStgs = 
                ConfigurationManager.AppSettings;

            string[] names = 
                ConfigurationManager.AppSettings.AllKeys;

            String value = String.Empty;

            for (int i = 0; i < appStgs.Count; i++)
            {


                string key = names[i];
				
                value = (String)reader.GetValue(key, value.GetType());

                sb.AppendFormat("#{0} Name: {1} Value: {2}",i, key, value);

            }
            
            return sb.ToString();

        }
        private SettingsHelper()
        {
            //foo
        }
        /// <summary>
        /// Returns the user config path.
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultExeConfigPath ()
        {
        	ConfigurationUserLevel userLevel = ConfigurationUserLevel.PerUserRoamingAndLocal;
        	
            
            try
            {

                var UserConfig = ConfigurationManager.OpenExeConfiguration (userLevel);
        		if (!System.IO.File.Exists (UserConfig.FilePath))
                {
        			return ShowAppSettings ();

                    
                }
        		return UserConfig.FilePath;
        	}
            catch (ConfigurationException e)
            {
        		return e.Filename;
        	}
			catch (Exception ex)
			{
        		logger.Error (ex.Message);
        		return "fuck you";
			}
        }
        /// <summary>
        /// Returns the user config path.
        /// </summary>
        /// <returns></returns>
        public static string GetDefaultExeConfigPath (ConfigurationUserLevel userLevel)
        {
        	try
            {
        		
                var UserConfig = ConfigurationManager.OpenExeConfiguration (userLevel);
        		if (UserConfig.FilePath.Length > 1)
				{
        			return UserConfig.FilePath;
        		}
				else {
					return "/home/dpl/tab2sql/tab2sql/Properties/Settings.settings";
        			//return System.IO.Directory.GetCurrentDirectory ();
        		}
        	}
			
            catch (ConfigurationException e)
            {
        		logger.Error("ConfigurationException:{0}", e.Message);
                return e.Filename;
            }
			catch (Exception ex) {
				logger.Error (ex.Message);
				return "/home/dpl/tab2sql/tab2sql/Properties/Settings.settings";
			}
        }
    }
}
